package com.task.util;

import org.apache.commons.collections4.CollectionUtils;

import java.net.URL;
import java.security.SecureRandom;
import java.util.Collection;
import java.util.StringJoiner;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class StringUtils {

    public static final String algorithmTitleRegex = "(([a-z]|_)(\\w*)){1,}";

    /**
     * method description: 获取字符串占用存储长度
     */
    public static int lengthOfUTF8(String value) {
        int valueLength = 0;
        if (value == null) {
            return valueLength;
        }
        String chinese = "";
        /* 获取字段值的长度，如果含中文字符，则每个中文字符长度为3，否则为1 */
        for (int i = 0; i < value.length(); i++) {
            /* 获取一个字符 */
            String temp = value.substring(i, i + 1);
            /* 判断是否为中文字符 */
            if (temp.matches(chinese)) {
                /* 中文字符长度为3 */
                valueLength += 3;
            } else {
                /* 其他字符长度为1 */
                valueLength += 1;
            }
        }
        return valueLength;
    }

    public static void main(String[] args) {
        System.out.println(lengthOfUTF8("你猜有多长，haha22"));
    }


    private static Pattern linePattern = Pattern.compile("_(\\w)");

    /**
     * 下划线转驼峰
     */
    public static String lineToHump(String str) {
        str = str.toLowerCase();
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    public static <T> String join(String delimiter, Collection<T> collection) {
        if (null == delimiter || CollectionUtils.isEmpty(collection)) {
            return null;
        }
        StringJoiner stringJoiner = new StringJoiner(delimiter);
        collection.forEach(t -> {
            if (null != t) {
                stringJoiner.add(t.toString());
            }
        });
        return stringJoiner.toString();
    }

    /**
     * 去除字符串前某一字符
     **/
    public static String trimLeft(String str, char c) {
        char[] value = str.toCharArray();
        int len = value.length;
        int st = 0;
        char[] val = value;

        while ((st < len) && (val[st] == c)) {
            st++;
        }
        return ((st > 0) || (len < value.length)) ? new String(value, st, len - st) : str;
    }

    /**
     * *去除字符串中特殊字符（文件命名中不支持的字符），
     */
    public static String transFileString(String str) {
        String cleanedFileName = str.replaceAll("[\\\\/:*?\"<>|]", "");
        return cleanedFileName;
    }

    /**
     * 生成指定长度的验证码
     *
     * @param length
     * @return String
     * @author Q
     * @since 2016-09-6
     */
    public static final String getUniqueCode(int length) {
        String[] charArrary = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
        String code = "";
        SecureRandom random = new SecureRandom();
        for (int i = 0; i < length; i++) {
            code += charArrary[random.nextInt(charArrary.length)];
        }
        return code;
    }

    public static String getUUIDStr() {
        UUID localUUID = UUID.randomUUID();
        String str = localUUID.toString();
        str = str.replaceAll("[-]", "");
        return str.toUpperCase();
    }

    public static boolean urlIsValid(String url) {
        try {
            new URL(url).toURI();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

}
